<?php
/**
 *
 * 版权所有：恰维网络<qwadmin.qiawei.com>
 * 作    者：寒川<hanchuan@qiawei.com>
 * 日    期：2016-09-20
 * 版    本：1.0.0
 * 功能说明：文章控制器。
 *
 **/

namespace Qwadmin\Controller;

use Vendor\Tree;

class BranchMenuController extends ComController
{

    public function add()
    {
        $this->display('form');
    }

    public function index($sid = 0, $p = 1)
    {
        $p = intval($p) > 0 ? $p : 1;

        $branch = M('branch_menu');
        $pagesize = 20;#每页数量
        $offset = $pagesize * ($p - 1);//计算记录偏移量
        $prefix = C('DB_PREFIX');
        $keyword = isset($_GET['keyword']) ? ($_GET['keyword']) : '';
        $order = isset($_GET['order']) ? $_GET['order'] : 'DESC';
        $where = '1 = 1 ';

        $list = D('branch_menu')->where("title like '%$keyword%'")->select();

        if ($keyword) {
            $where .= "and {$prefix}branch_menu.title like '%{$keyword}%' ";
        }
        //默认按照增加顺序降序
        $orderby = "id desc";
        if ($order == "asc") {
            $orderby = "id asc";
        }
        $count = $branch->where($where)->count();
        $list = $branch->where($where)->order($orderby)->limit($offset . ',' . $pagesize)->select();
       // echo D()->getLastSql();die;
        $page = new \Think\Page($count, $pagesize);
        $page = $page->show();
        $this->assign('list', $list);
        $this->assign('page', $page);
        $this->display();
    }

    public function del()
    {

        $ids = isset($_REQUEST['ids']) ? $_REQUEST['ids'] : false;
        if ($ids) {
            if (is_array($ids)) {
                $ids = implode(',', $ids);
                $map['id'] = array('in', $ids);
            } else {
                $map = 'id=' . $ids;
            }
            if (M('branch_menu')->where($map)->delete()) {
                addlog('删除Api链接，AID：' . $ids);
                $this->success('恭喜，删除成功！');
            } else {
                $this->error('参数错误！');
            }
        } else {
            $this->error('参数错误！');
        }

    }

    public function edit($id)
    {

        $id = intval($id);
        $branch = M('branch_menu')->where('id=' . $id)->find();
        if($branch){
            $this->assign('branch',$branch);
        }
        $this->display('form');
    }

    public function update($id = 0)
    {
        $id = intval($id);
        $data['title'] = isset($_POST['title']) ? $_POST['title'] : false;
        $data['description'] = isset($_POST['description']) ? $_POST['description'] : '';
        $data['params'] = isset($_POST['params']) ? $_POST['params'] : '';
        $data['url'] = isset($_POST['url']) ? $_POST['url'] : '';
        $data['type'] = isset($_POST['type']) ? $_POST['type'] : '';

        if (!$data['title'] or !$data['url']) {
            $this->error('警告！API标题、API链接为必填项目。');
        }
        if ($id) {
            M('branch_menu')->data($data)->where('id=' . $id)->save();
            addlog('编辑分支api，AID：' . $id);
            $this->success('恭喜！编辑成功！');
        } else {
            echo '新增';
            $aid = M('branch_menu')->data($data)->add();
            if ($aid) {
                addlog('新增分支api，AID：' . $id);
                $this->success('恭喜！新增成功！');
            } else {
                $this->error('抱歉，未知错误！');
            }

        }
    }
}